<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { luckDrawApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading, allNumber } from '@/utils/public';
  import serachUserid from '@/components/serachUserid/index.vue';
  import serachGift from '@/components/serachGift/index.vue'
  const formLabelWidth = '160px'
  const dataForm = ref(),
  serachGiftRef = ref();
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
  })
  interface formQuert {
    user_id:number
    gift_id:number
    pool_level:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    user_id: [{ required: true, message: "请输入用户UUID", trigger: "blur" }],
    gift_id: [{ required: true, message: "请输入礼物ID", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      await luckDrawApi.wishLuckAddPoolWhite(form).then(()=>{
        loading.close()
        ToastSuccess('新增礼物池白名单成功')
        $emit('updateList')
        closeDialog()
      }).catch(()=>{
        loading.close()
      })
    })
  }
  const getGiftList = async()=>{
    const res = await luckDrawApi.wishLuckPoolGift({
      pool_level:form.pool_level
    })
    const { data=[] } =res
    const list = data.map((res:any)=>{
      return {
        ...res,
        name:res.gift_name
      }
    })
    serachGiftRef.value.setList(list)
  }
  const openDialog = (query:any)=> {
    const { pool_level=0 } = query
    if(!pool_level) return ToastWarning('缺少 pool_level')
    form.pool_level = pool_level
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="40%"
      title="新增用户白名单">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" prop="user_id" label="用户UUID">
          <serachUserid v-model:user_id="form.user_id" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" prop="gift_id" label="礼物ID">
          <serachGift ref="serachGiftRef" v-model:gift_id="form.gift_id" :externalApi="true" @getList="getGiftList" />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
